package com.dongge;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sun.applet.AppletPanel;

/**
 * Created by Administrator on 2016/7/3.
 */
public class SpringShiro {

    /**
     * 这是第一种spring集成方法
     */
    @Test
    public void getSecurityManager(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        SecurityManager securityManager = (DefaultSecurityManager)applicationContext.getBean("securityManager");
        System.out.println(securityManager);
    }

    @Test
    public void getSubject(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Subject subject = SecurityUtils.getSubject();
        System.out.println(subject);
        UsernamePasswordToken token = new UsernamePasswordToken("zhang","123");
        try{
            subject.login(token);
            System.out.println("登录成功");
        }catch (AuthenticationException e){
            //登录失败
            System.out.println("登录失败");
        }

        subject.logout();
    }

    /**
     * 使用常规官网的方法，初始化的SecurityManager不会去读取ini配置文件
     * 本例通过测试完成对其进行修改
     */
    @Test
    public void getSubjectByIni(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml");
        Subject subject = SecurityUtils.getSubject();
        System.out.println(subject);
        UsernamePasswordToken token = new UsernamePasswordToken("zhang","123");
        try{
            subject.login(token);
            System.out.println("登录成功");
        }catch (AuthenticationException e){
            System.out.println("登录失败");
        }

        subject.logout();
    }

    /**
     * 本例用来测试用户的角色
     */
    @Test
    public void getRole(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang","123");
        try{
            subject.login(token);
            System.out.println("登录成功");
        }catch (AuthenticationException e){
            System.out.println("登录失败");
        }
        System.out.println(subject.hasRole("admin"));
        subject.logout();
    }

    /**
     * 测试多个Realm
     */
    @Test
    public void testMultiRealms(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml");
//        Subject subject = SecurityUtils.getSubject();
        Subject subject = (Subject)applicationContext.getBean("subject");
        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        try{
            subject.login(token);
            System.out.printf("登录成功");
        }catch (AuthenticationException e){
            System.out.println("登录失败");
        }

    }

    /**
     * 测试Realm提供授权信息
     */
    @Test
    public void testCustomAuthorizationRealm(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext4.xml");
        Subject subject = (Subject)applicationContext.getBean("subject");
        UsernamePasswordToken token = new UsernamePasswordToken("dongge","123456");
        try{
            subject.login(token);
            System.out.println("登录成功");
            if(subject.hasRole("admin")){
                System.out.println("admin role");
            }
        }catch (AuthenticationException e){
            System.out.println("登录失败");
        }
    }
}
